package com.hyfd.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.DateTimeUtils;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class SaiYueFlowTask {

    Logger log = Logger.getLogger(getClass());

    @Autowired
    OrderDao orderDao;
    @Autowired
    RabbitMqProducer mqProducer;//消息队列生产者
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息

//    @Scheduled(fixedDelay = 60000)
    public void queryZhiTongOrder(){
        Map<String,Object> map = new HashMap<String,Object>();
        int flag = -1;
        try {
            String providerId = "0000000090";//
            Map<String,Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);//获取通道
            String defaultParameter = (String) channel.get("default_parameter");//参数
            Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String queryUrl = paramMap.get("queryUrl");//充值地址
            String confId = paramMap.get("confId");//平台申请的独立 API账号
            String apiKey = paramMap.get("apiKey");//平台的appKey
            //查询订单中属于指通的折扣
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String,Object>> orderList = orderDao.selectByTask(param);//查询出所有尚通的充值中的订单
            for(int i = 0 , size = orderList.size() ; i < size ; i ++){
                Map<String,Object> order = orderList.get(i);
                String orderId = order.get("orderId")+"";//平台订单号
                map.put("orderId", orderId);
                long t = new Date().getTime()/1000;
                String md5Str = apiKey + confId + orderId + t;
                String sign = MD5.ToMD5(md5Str);
                String paramStr = "confId=" + confId + "&seqNo=" + orderId + "&t="+ t + "&sign="+sign;
                String result = ToolHttp.get(false, queryUrl+"?"+paramStr);
                //判断是否成功
                if(result!=null && !result.equals("")){
                    JSONObject resultJson = JSONObject.parseObject(result);//将返回值转为json对象
                    String retCode = resultJson.getString("retCode");//返回状态码
                    String retMsg = resultJson.getString("retMsg");//返回信息
                    String status = resultJson.getString("status");//充值状态
                    map.put("resultCode", status + ":" + retMsg);
                    if("0".equals(retCode)){
                        if("3".equals(status)){
                            flag = 1;
                        }else if("4".equals(status)){
                            flag = 0;
                        }else if("2".equals(status)||"1".equals(status)){
                            continue;
                        }
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        } catch (Exception e) {
            log.error("赛越流量查询出错"+e);
        }
    }
}
